//
//  ViewController.swift
//  computeDemo
//
//  Created by 陈 on 2017/4/20.
//  Copyright © 2017年 陈立华. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    var tf1: UITextField?
    var tf2: UITextField?
    var lb3: UILabel?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        
    }
    
    
    func setupUI()->(){
        
        view.backgroundColor = UIColor.gray
        
        let tf1 = UITextField(frame: CGRect(x: 10, y: 40, width: 100, height: 30))
        tf1.borderStyle = .roundedRect
        tf1.text = "0"
        view.addSubview(tf1)
        self.tf1 = tf1

        let lb1 = UILabel(frame: CGRect(x: 110, y: 40, width: 30, height: 30))
        lb1.backgroundColor = UIColor.gray
        lb1.textAlignment = .center
        lb1.text = "+"
        view.addSubview(lb1)
        
        
        let tf2 = UITextField(frame: CGRect(x: 140, y: 40, width: 100, height: 30))
        tf2.borderStyle = .roundedRect
        tf2.text = "0"
        view.addSubview(tf2)
        self.tf2 = tf2
        
        let lb2 = UILabel(frame: CGRect(x: 240, y: 40, width: 30, height: 30))
        lb2.textAlignment = .center
        lb2.text = "="
        view.addSubview(lb2)
        
        let lb3 = UILabel(frame: CGRect(x: 270, y: 40, width: 100, height: 30))
        lb3.text = "0"
        lb3.textAlignment = .right
        view.addSubview(lb3)
        self.lb3 = lb3
        
        let computeBtn = UIButton(type: .custom)
        computeBtn.addTarget(self, action: #selector(computeBtnClick), for: .touchUpInside)
        computeBtn.backgroundColor = UIColor.blue
        computeBtn.frame = CGRect(x: 100, y: 100, width: 60, height: 40)
        computeBtn.setTitle("计算", for: .normal)
        view.addSubview(computeBtn)
        
        
        
        let zeroBtn = UIButton(type: .custom)
        
        zeroBtn.addTarget(self, action: #selector(zeroBtnClick), for: .touchUpInside)
        zeroBtn.frame = CGRect(x: 200, y: 100, width: 60, height: 40)
        zeroBtn.backgroundColor = UIColor.blue
        zeroBtn.setTitle("清零", for: .normal)
        view.addSubview(zeroBtn)
    }
    
    func zeroBtnClick()->(){
        
        self.tf1?.text = "0"
        self.tf2?.text = "0"
        self.lb3?.text = "0"
        
    }
    
    func computeBtnClick()->() {
        
        let number1 = Double(self.tf1?.text ?? "0")
        
        let number2 = Double(self.tf2?.text ?? "0")
        
        
        let resultNumber = (number1 ?? 0) + (number2 ?? 0)
        
        self.lb3?.text = "\(resultNumber)"
        
    }
}

